//
//  CDCodeLoginStep.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/4/22.
//

import Combine

struct CDCodeLoginStep: LoginStep {
    struct InputData {
        let phonenumber: String
        let smsCode: String
    }
     func execute(_ input: InputData) -> AnyPublisher<AuthAPI.SMSLogin.Response, Error> {
        Future { promise in
            let api = AuthAPI.SMSLogin(body: .init(phonenumber: input.phonenumber, smsCode: input.smsCode))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                case .success(let response):
                    if let response = response.data
                    {
                        Logger.log(.info, message: "验证码登陆成功: \(response)",tag: .login)
                        promise(.success(response))
                    }
                    else{
                        promise(.failure(NSError(domain: "验证码登录", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                case .failure(let error):
                    promise(.failure(error))
                }
            })
        }
        .eraseToAnyPublisher()
    }
}
